﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.astd.common.manager;
using com.lover.common.json;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class DuowanLogin : LoginImplBase
    {
        public DuowanLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            HttpResult resp;
            string content = "";

            ////////////start login
            string loginurl = "https://udb.duowan.com/login.do";
            string data = String.Format("url=&username={0}&password={1}&securityCode=", _username, _password);
            resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string redirecturl = String.Format("http://udblogin.duowan.com/login.do?game=AS&server=s{0}&pid=test&from=webyygame&webyygame", _acc.ServerId);
            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            content = resp.getContent();
            Match m = Regex.Match(content, "<iframe src=['\"]+([^'\"]*)['\"]+");
            if (m == null || m.Groups == null || m.Groups.Count < 2)
            {
                result.StatusCode = LoginStatusCode.FailInLogin; 
                return result;
            }
            string gameurl = m.Groups[1].Value;

            processStartGame(gameurl, result, ref cookies);
            return result;
        }
    }
}
